<?php


namespace app\api\controller;

use app\common\model\Post as P;
use app\common\model\Comment;
use think\facade\Db;

class Post extends Base
{

    public function postList()
    {
        $sessionKey = input("sessionKey");
        $where = [];
        if (!empty($sessionKey)){
            $uid = $this->getUid($sessionKey);
            $where["tn_post.uid"] = $uid;
        }
        $list = P::withJoin(['userInfo'	=>	['username','avatar']])->where($where)->order("id desc")->paginate(10,false,['query'=>request()->param()])->each(function($item, $key){
           $item["startCount"] = Db::name("fabulous")->where("type",2)->where("aid",$item["id"])->count();
            if (!empty($item["media"])){
               $item["media"] = explode(",",$item["media"]);
               return $item;
           }else{
               $media = array();
               array_push($media,"https://" .request()->host() ."/static/images/api/post_default_cover_".rand(1,8).".jpg");
               $item["media"] = $media;

           }
        });
        return json($list);
    }

    public function addPost()
    {
        $content = input("content");
        $sessionKey = input("sessionKey");
        if (empty($sessionKey)){
            return json(["code" => 0 ,"sessionKey为空"]);
        }

        if ($this->checkText($content) != 0){
            return json(["code" => 0 ,"msg" =>"内容违法违规"]);
        }
        $this->checkText($content);
        $uid = $this->getUid($sessionKey);
        $addPost = P::create(["uid" =>$uid,"content" => $content]);
        if ($addPost){
            return json(["code" => 1,"pid" =>$addPost->id,"msg" => "发布成功"]);
        }
        return json(["code" => 0,"msg" =>"发布失败"]);
    }

    public function delPost()
    {
        $sessionKey = input("sessionKey");
        if (empty($sessionKey)){
            return json(["code" => 0 ,"sessionKey为空"]);
        }
        $id = input("id");
        $uid = $this->getUid($sessionKey);
        $where["uid"] = $uid;
        $where["id"] = $id;
        $isEmpty = P::where($where)->find();
        if (!$isEmpty){
            return json(["code" => 0,"msg" =>"只可删除自己发布的动态"]);
        }

        $res = P::destroy($id);
        if ($res){
            return json(["code" => 1,"msg" =>"删除成功"]);
        }
        return json(["code" => 0,"msg" =>"删除失败"]);
    }

    public function updateImage()
    {
        $pid = input("pid");
        $media = input("media");
        $res = P::update(["media" =>$media],["id" => $pid]);
        if ($res){
            return json(["code" => 1,"msg" =>"发布成功"]);
        }
        return json(["code" => 0,"code" =>"发布失败"]);
    }

    public  function detail()
    {
        $id = input("id");
        $res = P::withJoin(['userInfo'	=>	['username','avatar','intro']])->find($id);
        $comment = Comment::withJoin(['userInfo' => ['username','avatar']])->where("type",2)->order("id desc")->where("aid",$id)->select();
        $res["comment"] = $comment;

        if (!empty($res["media"])){
            $res["media"] = explode(",",$res["media"]);
        }else{
            $media = array();
            array_push($media,"https://" .request()->host() ."/static/images/api/post_default_cover_".rand(1,8).".jpg");
            $res["media"] = $media;
        }

        return json($res);
    }
}